package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.business.domain.*;
import com.ruoyi.business.domain.dto.*;
import com.ruoyi.business.domain.enums.*;
import com.ruoyi.business.domain.vo.FrontLineChartVO;
import com.ruoyi.business.domain.vo.FrontNewScoreVO;
import com.ruoyi.business.domain.vo.FrontPieChartVO;
import com.ruoyi.business.domain.vo.PointVO;
import com.ruoyi.business.mapper.*;
import com.ruoyi.business.utils.Standard;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.service.IScoreService;
import org.springframework.util.ObjectUtils;

/**
 * 成绩Service业务层处理
 * 
 * @author lcz
 * @date 2025-02-23
 */
@Service
public class ScoreServiceImpl implements IScoreService 
{
    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentBindingMapper studentBindingMapper;

    @Autowired
    private ProjectStandardMapper projectStandardMapper;

    @Autowired
    private SchoolProjectStandardMapper schoolProjectStandardMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private TestSessionMapper testSessionMapper;

    @Autowired
    private ScoringStandardMapper scoringStandardMapper;

    /**
     * 查询成绩
     * 
     * @param id 成绩主键
     * @return 成绩
     */
    @Override
    public Score selectScoreById(Long id)
    {
        return scoreMapper.selectScoreById(id);
    }

    /**
     * 查询成绩列表
     * 
     * @param score 成绩
     * @return 成绩
     */
    @Override
    public List<Score> selectScoreList(Score score)
    {
        if (!ObjectUtils.isEmpty(score.getStartTime())){
            LocalDate temp = LocalDate.parse(score.getStartTime());
            score.setStartLocalDateTime(temp.atStartOfDay());
        }
        if (!ObjectUtils.isEmpty(score.getEndTime())){
            LocalDate temp = LocalDate.parse(score.getEndTime());
            score.setEndLocalDateTime(temp.plusDays(1).atStartOfDay());
        }
        return scoreMapper.selectScoreList(score);
    }

    /**
     * 新增成绩
     * 
     * @param score 成绩
     * @return 结果
     */
    @Override
    public int insertScore(Score score)
    {
        score.setCreateTime(DateUtils.getNowDate());
        return scoreMapper.insertScore(score);
    }

    /**
     * 修改成绩
     * 
     * @param score 成绩
     * @return 结果
     */
    @Override
    public int updateScore(Score score)
    {
        score.setUpdateTime(DateUtils.getNowDate());
        return scoreMapper.updateScore(score);
    }

    /**
     * 批量删除成绩
     * 
     * @param ids 需要删除的成绩主键
     * @return 结果
     */
    @Override
    public int deleteScoreByIds(Long[] ids)
    {
        return scoreMapper.deleteScoreByIds(ids);
    }

    /**
     * 删除成绩信息
     * 
     * @param id 成绩主键
     * @return 结果
     */
    @Override
    public int deleteScoreById(Long id)
    {
        return scoreMapper.deleteScoreById(id);
    }

    @Override
    public R<FrontNewScoreVO> frontNewScore(FrontNewScoreDTO dto) {
        Long studentId = 0l;
        if (ObjectUtils.isEmpty(dto.getStudentId())){
            studentId = SecurityUtils.getLoginUser().getUser().getUserId();
        }else {
            studentId = dto.getStudentId();
        }

        FrontNewScoreVO frontNewScoreVO = new FrontNewScoreVO();
        Map<String, Score> latestScores = new HashMap<>();
        List<String> projectTypes = List.of(
                ProjectTypeEnum.HEIGHT.getCode(),
                ProjectTypeEnum.LUNG_CAPACITY.getCode(),
                ProjectTypeEnum.SIT_AND_REACH.getCode(),
                ProjectTypeEnum.FIFTY_METER_DASH.getCode(),
//                ProjectTypeEnum.LONG_DISTANCE_RUN.getCode(),
                ProjectTypeEnum.SIT_UPS.getCode(),
                ProjectTypeEnum.STANDING_LONG_JUMP.getCode(),
                ProjectTypeEnum.SKIP_ROPE.getCode(),
                ProjectTypeEnum.RUN_100.getCode(),
                ProjectTypeEnum.RUN_200.getCode(),
                ProjectTypeEnum.RUN_400.getCode(),
                ProjectTypeEnum.RUN_800.getCode(),
                ProjectTypeEnum.RUN_1000.getCode(),
                ProjectTypeEnum.RUN_1500.getCode(),
                ProjectTypeEnum.RUN_3000.getCode()
        );

        for (String projectType : projectTypes) {
            List<Score> scores = scoreMapper.findLatestScoresByStudentIdAndProjectType(studentId, projectType);
            if (!scores.isEmpty()) {
                latestScores.put(projectType, scores.get(0));
            }
        }

        frontNewScoreVO.setHeightWeight(latestScores.get(ProjectTypeEnum.HEIGHT.getCode()));
        frontNewScoreVO.setLungCapacity(latestScores.get(ProjectTypeEnum.LUNG_CAPACITY.getCode()));
        frontNewScoreVO.setSitAndReach(latestScores.get(ProjectTypeEnum.SIT_AND_REACH.getCode()));
        frontNewScoreVO.setFiftyMeterDash(latestScores.get(ProjectTypeEnum.FIFTY_METER_DASH.getCode()));
//        frontNewScoreVO.setLongDistanceRun(latestScores.get(ProjectTypeEnum.LONG_DISTANCE_RUN.getCode()));
        frontNewScoreVO.setSitUps(latestScores.get(ProjectTypeEnum.SIT_UPS.getCode()));
        frontNewScoreVO.setStandingLongJump(latestScores.get(ProjectTypeEnum.STANDING_LONG_JUMP.getCode()));
        frontNewScoreVO.setSkipRope(latestScores.get(ProjectTypeEnum.SKIP_ROPE.getCode()));
        frontNewScoreVO.setRun100(latestScores.get(ProjectTypeEnum.RUN_100.getCode()));
        frontNewScoreVO.setRun200(latestScores.get(ProjectTypeEnum.RUN_200.getCode()));
        frontNewScoreVO.setRun400(latestScores.get(ProjectTypeEnum.RUN_400.getCode()));
        frontNewScoreVO.setRun800(latestScores.get(ProjectTypeEnum.RUN_800.getCode()));
        frontNewScoreVO.setRun1000(latestScores.get(ProjectTypeEnum.RUN_1000.getCode()));
        frontNewScoreVO.setRun1500(latestScores.get(ProjectTypeEnum.RUN_1500.getCode()));
        frontNewScoreVO.setRun3000(latestScores.get(ProjectTypeEnum.RUN_3000.getCode()));

        return R.ok(frontNewScoreVO);
    }

    @Override
    public R<FrontLineChartVO> frontLineChart(FrontLineChartDTO dto) {
        if (ObjectUtils.isEmpty(dto.getStudentId())){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            if (!user.getUserType().equals(UserTypeEnum.STUDENT.getKey())){
                return R.fail("非学生用户需要传入学生id");
            }
            dto.setStudentId(user.getUserId());
        }
        FrontLineChartVO lineChartVO = new FrontLineChartVO();
        lineChartVO.setProjectType(dto.getType());
        switch (dto.getType()) {
            case "week":
                List<PointVO> weekMaxList = scoreMapper.getWeekMaxList(dto);
                List<PointVO> weekMinList = scoreMapper.getWeekMinList(dto);
                lineChartVO.setMaxList(weekMaxList);
                lineChartVO.setMinList(weekMinList);
                break;
            case "month":
                List<PointVO> monthMaxList = scoreMapper.getMonthMaxList(dto);
                List<PointVO> monthMinList = scoreMapper.getMonthMinList(dto);
                lineChartVO.setMaxList(monthMaxList);
                lineChartVO.setMinList(monthMinList);
                break;
            case "year":
                List<PointVO> yearMaxList = scoreMapper.getYearMaxList(dto);
                List<PointVO> yearMinList = scoreMapper.getYearMinList(dto);
                lineChartVO.setMaxList(yearMaxList);
                lineChartVO.setMinList(yearMinList);
                break;
            default:
                return R.fail("时间维度错误！");
        }
        return R.ok(lineChartVO);
    }

    @Override
    public List<Score> frontListByTestSession(FrontListByTestSessionDTO dto) {
        if (dto.getProjectType().equals(ProjectTypeEnum.FIFTY_METER_DASH.getCode())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_100.getProjectType())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_200.getProjectType())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_400.getProjectType())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_800.getProjectType())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_1000.getProjectType())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_1500.getProjectType())
                || dto.getProjectType().equals(ProjectTypeEnum.RUN_3000.getProjectType())
//                ||dto.getProjectType().equals(ProjectTypeEnum.LONG_DISTANCE_RUN.getCode())
        ){
            return scoreMapper.frontListByTestSessionRun(dto);
        }
        return scoreMapper.frontListByTestSession(dto);
    }

    @Override
    public R<FrontLineChartVO> frontLineChartByClassId(FrontLineChartByClassIdDTO dto) {
        User param = new User();
        param.setClassId(dto.getClassId());
        List<User> userList = userMapper.selectUserList(param);
        List<Long> studentIds = userList.stream().map(User::getId).collect(Collectors.toList());
        FrontLineChartVO lineChartVO = new FrontLineChartVO();
        lineChartVO.setProjectType(dto.getType());
        switch (dto.getType()) {
            case "week":
//                List<PointVO> weekMaxList = scoreMapper.getWeekMaxList(dto);
//                List<PointVO> weekMinList = scoreMapper.getWeekMinList(dto);
//                lineChartVO.setMaxList(weekMaxList);
//                lineChartVO.setMinList(weekMinList);
                break;
            case "month":
                List<PointVO> monthMaxList = scoreMapper.getAVGMaxWeekListByProjectType(dto.getProjectType().toString(),studentIds);
                List<PointVO> monthMinList = scoreMapper.getAVGMinWeekListByProjectType(dto.getProjectType().toString(),studentIds);

                for (int i = 0; i < monthMaxList.size(); i++) {
                    monthMaxList.get(i).setXAxis("第"+(i+1)+"周");
                }

                for (int i = 0; i < monthMinList.size(); i++) {
                    monthMinList.get(i).setXAxis("第"+(i+1)+"周");
                }

                lineChartVO.setMaxList(monthMaxList);
                lineChartVO.setMinList(monthMinList);
                break;
            case "year":
                List<PointVO> yearMaxList = scoreMapper.getAVGMaxListByProjectType(dto.getProjectType().toString(),studentIds);
                List<PointVO> yearMinList = scoreMapper.getAVGMinListByProjectType(dto.getProjectType().toString(),studentIds);
                lineChartVO.setMaxList(yearMaxList);
                lineChartVO.setMinList(yearMinList);
                break;
            default:
                return R.fail("时间维度错误！");
        }
        return R.ok(lineChartVO);
    }

    @Override
    public R<List<FrontPieChartVO>> frontPieChart(Long testSessionId, String projectType,Long classId) {

        List<FrontPieChartVO> list = scoreMapper.getFrontPieChart(testSessionId,projectType,classId);

        if (projectType.equals(ProjectTypeEnum.FIFTY_METER_DASH.getCode())
                || projectType.equals(ProjectTypeEnum.RUN_100.getProjectType())
                || projectType.equals(ProjectTypeEnum.RUN_200.getProjectType())
                || projectType.equals(ProjectTypeEnum.RUN_400.getProjectType())
                || projectType.equals(ProjectTypeEnum.RUN_800.getProjectType())
                || projectType.equals(ProjectTypeEnum.RUN_1000.getProjectType())
                || projectType.equals(ProjectTypeEnum.RUN_1500.getProjectType())
                || projectType.equals(ProjectTypeEnum.RUN_3000.getProjectType())
//                ||projectType.equals(ProjectTypeEnum.LONG_DISTANCE_RUN.getCode())
        ){
            list = scoreMapper.getFrontPieChartRun(testSessionId,projectType,classId);
        }

        double total = list.stream().mapToDouble(FrontPieChartVO::getTotalStudents).sum();
        BigDecimal sumOfPercentages = BigDecimal.ZERO;

        if (list.size()<=0){
            return R.ok();
        }
        for (int i = 0;i<list.size();i++) {
            if (i < list.size() - 1) {
                BigDecimal percentage = BigDecimal.valueOf(list.get(i).getTotalStudents()).multiply(BigDecimal.valueOf(100))
                        .divide(BigDecimal.valueOf(total), 2, RoundingMode.HALF_UP); // 保留2位小数
                list.get(i).setPercentage(percentage);
                sumOfPercentages = sumOfPercentages.add(percentage);
            }else {
                list.get(i).setPercentage(BigDecimal.valueOf(100).subtract(sumOfPercentages));
            }
        }
        return R.ok(list);
    }

    @Override
    public void addScore(HardwareDataReceptionDTO dto) {
        System.out.println(JSON.toJSONString(dto));
        StudentBinding param = new StudentBinding();
        param.setCode(dto.getTraineeId());
        param.setSchoolId(Long.valueOf(dto.getCustomerId()));
        StudentBinding studentBinding = studentBindingMapper.selectListOrderByDescId(param);
        School school = schoolMapper.selectSchoolById(studentBinding.getSchoolId());
        if (!ObjectUtils.isEmpty(studentBinding)) {
            // 查询学生信息
            User user = userMapper.selectUserById(studentBinding.getStudentId());
            TestSession testSession = testSessionMapper.selectTestSessionById(studentBinding.getTestSessionId());
            if (ObjectUtils.isEmpty(user)) {
                return;
            }

            Score score = new Score();
            ProjectTypeEnum projectType = ProjectTypeEnum.getEnumByProjectType(dto.getSports());
            score.setProjectType(projectType.getCode());
            score.setProjectName(projectType.getValue());
            score.setStudentId(user.getId());
            score.setStudentName(user.getUserName());
            score.setGender(user.getGender());
            score.setGrade(user.getGrade());
            score.setGradestr(user.getGradename());
            score.setTestSessionId(studentBinding.getTestSessionId());
            score.setTestSessionName(testSession.getTestName());
            score.setSchoolId(school.getId());
            score.setSchoolName(school.getSchoolName());
            score.setClassId(user.getClassId());
            score.setClassName(user.getClassName());

            List<Standard> standards = getStandards(ProjectTypeEnum.getEnumByProjectType(dto.getSports()).getCode(), user);

            determineGradeAndScore(score, standards, dto);
        }
    }



//    @Override
//    public void addScore(HardwareDataReceptionDTO dto) {
////        System.out.println(JSON.toJSONString(dto));
//        StudentBinding param = new StudentBinding();
//        param.setCode(dto.getTraineeId());
//        param.setSchoolId(Long.valueOf(dto.getCustomerId()));
//        StudentBinding studentBinding = studentBindingMapper.selectListOrderByDescId(param);
//        School school = schoolMapper.selectSchoolById(studentBinding.getSchoolId());
//        if (!ObjectUtils.isEmpty(studentBinding)) {
//            // 查询学生信息
//            User user = userMapper.selectUserById(studentBinding.getStudentId());
//            TestSession testSession = testSessionMapper.selectTestSessionById(studentBinding.getTestSessionId());
//            if (ObjectUtils.isEmpty(user)) {
//                return;
//            }
//
//            Score score = new Score();
//            ProjectTypeEnum projectType = ProjectTypeEnum.getEnumByProjectType(dto.getSports());
//            score.setProjectType(projectType.getCode());
//            score.setProjectName(projectType.getValue());
//            score.setStudentId(user.getId());
//            score.setStudentName(user.getUserName());
//            score.setGender(user.getGender());
//            score.setGrade(user.getGrade());
//            score.setTestSessionId(studentBinding.getTestSessionId());
//            score.setTestSessionName(testSession.getTestName());
//            score.setSchoolId(school.getId());
//            score.setSchoolName(school.getSchoolName());
//            score.setClassId(user.getClassId());
//            score.setClassName(user.getClassName());
//
//            List<Standard> standards = getStandards(ProjectTypeEnum.getEnumByProjectType(dto.getSports()).getCode(), user);
//
//            determineGradeAndScore(score, standards, dto);
//        }
//    }

    private void determineGradeAndScore(Score score,List<Standard> standards, HardwareDataReceptionDTO dto) {

        BigDecimal scoreValue = BigDecimal.valueOf(Double.valueOf(dto.getScore()));

        if (!standards.isEmpty()) {
            if (dto.getSports().equals(ProjectTypeEnum.HEIGHT.getProjectType())){
                //身高
                Score scoreHave = scoreMapper.selectScoreByHaveWeight(score.getStudentId());
                if (ObjectUtils.isEmpty(scoreHave)) {
                    score.setHeight(scoreValue.divide(BigDecimal.valueOf(100)));
                    score.setCreateTime(new Date());
                    scoreMapper.insertScore(score);
                }else {
                    scoreHave.setHeight(scoreValue.divide(BigDecimal.valueOf(100)));
                    //校验身高体重数据是否齐全
                    if (!ObjectUtils.isEmpty(scoreHave.getWeight())) {
                        //计算BMI
                        BigDecimal bmi = scoreHave.getWeight().divide(scoreHave.getHeight().multiply(scoreHave.getHeight()), 2, RoundingMode.HALF_UP);
                        scoreHave.setScore(bmi);
                        //计算等级以及单项得分，并赋值
                        for (int i = 0; i < standards.size(); i++) {
                            BigDecimal standardValue = standards.get(i).getStandardValue();
                            if (standardValue.compareTo(bmi)==1) {
                                scoreHave.setGradeId(Long.valueOf(standards.get(i-1).getGradeId()));
                                scoreHave.setGradeName(standards.get(i-1).getGradeName());
                                scoreHave.setSingleScore(standards.get(i-1).getSingleScore());
                                break;
                            }
                            if (i >= standards.size()-1){
                                scoreHave.setGradeId(Long.valueOf(standards.get(i).getGradeId()));
                                scoreHave.setGradeName(standards.get(i).getGradeName());
                                scoreHave.setSingleScore(standards.get(i).getSingleScore());
                                break;
                            }
                        }
                    }
                    scoreHave.setCreateTime(new Date());
                    scoreMapper.updateScore(scoreHave);
                }

            }else if(dto.getSports().equals(ProjectTypeEnum.WEIGHT.getProjectType())) {
                //体重
                Score scoreHave = scoreMapper.selectScoreByHaveHeight(score.getStudentId());
                if (ObjectUtils.isEmpty(scoreHave)) {
                    score.setWeight(scoreValue);
                    score.setCreateTime(new Date());
                    scoreMapper.insertScore(score);
                } else {
                    scoreHave.setWeight(scoreValue);
                    //校验身高体重数据是否齐全
                    if (!ObjectUtils.isEmpty(scoreHave.getHeight())) {
                        //计算BMI
                        BigDecimal bmi = scoreHave.getWeight().divide(scoreHave.getHeight().multiply(scoreHave.getHeight()), 2, RoundingMode.HALF_UP);
                        scoreHave.setScore(bmi);
                        //计算等级以及单项得分，并赋值
                        for (int i = 0; i < standards.size(); i++) {
                            BigDecimal standardValue = standards.get(i).getStandardValue();
                            if (standardValue.compareTo(bmi) == 1) {
                                scoreHave.setGradeId(Long.valueOf(standards.get(i - 1).getGradeId()));
                                scoreHave.setGradeName(standards.get(i - 1).getGradeName());
                                scoreHave.setSingleScore(standards.get(i - 1).getSingleScore());
                                break;
                            }
                            if (i >= standards.size() - 1) {
                                scoreHave.setGradeId(Long.valueOf(standards.get(i).getGradeId()));
                                scoreHave.setGradeName(standards.get(i).getGradeName());
                                scoreHave.setSingleScore(standards.get(i).getSingleScore());
                                break;
                            }
                        }
                    }
                    scoreHave.setCreateTime(new Date());
                    scoreMapper.updateScore(scoreHave);
                }
            }else if (dto.getSports().equals(ProjectTypeEnum.FIFTY_METER_DASH.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_100.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_200.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_400.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_800.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_1000.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_1500.getProjectType())
                    || dto.getSports().equals(ProjectTypeEnum.RUN_3000.getProjectType())
//                    || dto.getSports().equals(ProjectTypeEnum.LONG_DISTANCE_RUN.getProjectType())
            ){
                //50米跑和长跑
                for (int i = 0; i < standards.size(); i++) {
                    BigDecimal standardValue = standards.get(i).getStandardValue();
                    if (standardValue.compareTo(scoreValue) >= 0) {
                        score.setGradeId(Long.valueOf(standards.get(i).getGradeId()));
                        score.setGradeName(standards.get(i).getGradeName());
                        score.setSingleScore(standards.get(i).getSingleScore());
                        break;
                    }
                    if (i >= standards.size()-1){
                        score.setGradeId(Long.valueOf(GradeLevelEnum.GRADE_4.getCode()));
                        score.setGradeName(GradeLevelEnum.GRADE_4.getValue());
                        score.setSingleScore(0l);
                        break;
                    }
                }
                score.setScore(scoreValue);
                score.setCreateTime(new Date());
                scoreMapper.insertScore(score);
            }else {
                //其他项目
                //计算等级以及单项得分，并赋值
                for (int i = 0; i < standards.size(); i++) {
                    BigDecimal standardValue = standards.get(i).getStandardValue();
                    if (standardValue.compareTo(scoreValue)==1) {
                        if (i==0){
                            score.setGradeId(Long.valueOf(GradeLevelEnum.GRADE_4.getCode()));
                            score.setGradeName(GradeLevelEnum.GRADE_4.getValue());
                            score.setSingleScore(0l);
                        }else {
                            score.setGradeId(Long.valueOf(standards.get(i-1).getGradeId()));
                            score.setGradeName(standards.get(i-1).getGradeName());
                            score.setSingleScore(standards.get(i-1).getSingleScore());
                        }
                        break;
                    }
                    if (i >= standards.size()-1){
                        score.setGradeId(Long.valueOf(standards.get(i).getGradeId()));
                        score.setGradeName(standards.get(i).getGradeName());
                        score.setSingleScore(standards.get(i).getSingleScore());
                        break;
                    }
                }
                score.setScore(scoreValue);
                score.setCreateTime(new Date());
                scoreMapper.insertScore(score);
            }

        }
    }

    private List<Standard> getStandards(String projectType, User user) {

        String gender = user.getGender();
        String grade = user.getGrade();
        Long schoolId = user.getSchoolId();

        ScoringStandard param = new ScoringStandard();
        param.setGender(gender);
        param.setProjectId(projectType);
        param.setGradeId(Long.valueOf(grade));
        param.setSchoolId(schoolId);
        List<ScoringStandard> scoringStandards = scoringStandardMapper.selectListOrderBy(param);

        List<Standard> standards = new ArrayList<>();
        if (!scoringStandards.isEmpty()) {
            standards = scoringStandards.stream().map(s -> new Standard(s.getLevelId().toString(), s.getLevelName(), s.getItemScore(), s.getScore())).collect(Collectors.toList());
        }
        return standards;

//        SchoolProjectStandard schoolParam = new SchoolProjectStandard();
//        schoolParam.setGender(gender);
//        schoolParam.setProjectType(projectType);
//        schoolParam.setSchoolId(schoolId);
//        List<SchoolProjectStandard> schoolStandards = new ArrayList<>();
//
//        ProjectStandard projectParam = new ProjectStandard();
//        projectParam.setGender(gender);
//        projectParam.setProjectType(projectType);
//        List<ProjectStandard> projectStandards = new ArrayList<>();
//
//        if (projectType.equals(ProjectTypeEnum.HEIGHT.getCode())){
//            schoolStandards = schoolProjectStandardMapper.selectHWListOrderBy(schoolParam);
//            projectStandards = projectStandardMapper.selectHWListOrderBy(projectParam);
//            return getStandardsChild(schoolStandards,projectStandards,grade);
//        }else {
//            schoolStandards = schoolProjectStandardMapper.selectProjectStandardListOrderBy(schoolParam);
//            projectStandards = projectStandardMapper.selectProjectStandardListOrderBy(projectParam);
//            return getStandardsChild(schoolStandards,projectStandards,grade);
//        }
    }

//    private List<Standard> getStandardsChild(List<SchoolProjectStandard> schoolStandards,List<ProjectStandard> projectStandards, String grade){
//        List<Standard> standards = new ArrayList<>();
//        if (!schoolStandards.isEmpty()) {
//            standards = schoolStandards.stream().map(s -> new Standard(s.getGradeId(), s.getGradeName(), s.getSingleScore(), getGradeStandard(s, grade))).collect(Collectors.toList());
//        } else if (!projectStandards.isEmpty()) {
//            standards = projectStandards.stream().map(p -> new Standard(p.getGradeId(), p.getGradeName(), p.getSingleScore(), getGradeStandard(p, grade))).collect(Collectors.toList());
//        }
//        return standards;
//    }

    private BigDecimal getGradeStandard(SchoolProjectStandard standard, String grade) {
        switch (grade) {
            case "1":
                return standard.getGradeOne();
            case "2":
                return standard.getGradeTwo();
            case "3":
                return standard.getGradeThree();
            case "4":
                return standard.getGradeFour();
            case "5":
                return standard.getGradeFive();
            case "6":
                return standard.getGradeSix();
            default:
                return BigDecimal.ZERO;
        }
    }

    private BigDecimal getGradeStandard(ProjectStandard standard, String grade) {
        switch (grade) {
            case "1":
                return standard.getGradeOne();
            case "2":
                return standard.getGradeTwo();
            case "3":
                return standard.getGradeThree();
            case "4":
                return standard.getGradeFour();
            case "5":
                return standard.getGradeFive();
            case "6":
                return standard.getGradeSix();
            default:
                return BigDecimal.ZERO;
        }
    }
}
